package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.util.SystemPropertyReplacerListener;

import entity.*;
import utils.JDBCUtil;

public class ArticalDaoImpl implements ArticalDao {

	@Override
	public List<Artical> findAllArtByLord(int lordId) {
		Connection conn = null;
		PreparedStatement pStatement = null;
		ResultSet rSet = null;
		List<Artical> articals = new ArrayList<Artical>();
		try {
			conn = JDBCUtil.getConnect();
			String sql = "select * from artical where lordId = ? order by floor ASC";
			pStatement = conn.prepareStatement(sql);
			pStatement.setInt(1, lordId);
			rSet = pStatement.executeQuery();
			while(rSet.next()) {
				Artical artical = new Artical();
				artical.setAuthor(rSet.getString(1));
				artical.setLordId(rSet.getInt(2));
				artical.setText(rSet.getString(3));
				artical.setTime(rSet.getString(4));
				artical.setFloor(rSet.getString(5));
				artical.setId(rSet.getInt(6));
				articals.add(artical);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(conn, pStatement, rSet);
		}
		return articals;
	}

	@Override
	public int countFloorByLord(int lordId) {
		Connection conn = null;
		PreparedStatement pStatement = null;
		ResultSet rSet = null;
		int count = 2;
		try {
			conn = JDBCUtil.getConnect();
			String sql = "select * from artical where lordId = ?";
			pStatement = conn.prepareStatement(sql);
			pStatement.setInt(1, lordId);
			rSet = pStatement.executeQuery();
			while(rSet.next()) {
				count++;
			}
			return count;
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(conn, pStatement, rSet);
		}
		return count;
	}

	@Override
	public void insertArtic(String author,String text,int lordId) {
		Connection conn = null;
		PreparedStatement pStatement = null;
		ResultSet rSet = null;
		try {
			conn = JDBCUtil.getConnect();
			String sql = "insert into artical (author,lordId,text,floor) values (?,?,?,?)";
			pStatement = conn.prepareStatement(sql);
			pStatement.setString(1, author);
			pStatement.setInt(2, lordId);
			pStatement.setString(3, text);
			int floor = countFloorByLord(lordId);
			pStatement.setInt(4, floor);
			pStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(conn, pStatement, rSet);
		}
	}

	@Override
	public List<Artical> findArticalByAuthor(String author) {
		Connection conn = null;
		PreparedStatement pStatement = null;
		ResultSet rSet = null;
		List<Artical> articals = new ArrayList<>();
		try {
			conn = JDBCUtil.getConnect();
			String sql = "select * from artical where author = ?";
			pStatement = conn.prepareStatement(sql);
			pStatement.setString(1, author);
			rSet = pStatement.executeQuery();
			while(rSet.next()) {
				Artical artical = new Artical();
				artical.setAuthor(rSet.getString(1));
				artical.setLordId(rSet.getInt(2));
				artical.setText(rSet.getString(3));
				artical.setTime(rSet.getString(4));
				artical.setFloor(Integer.toString(rSet.getInt(5)));
				artical.setId(rSet.getInt(6));
				articals.add(artical);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(conn, pStatement, rSet);
		}
		return articals;
	}

	@Override
	public List<Artical> fuzzyFindArtical(String word) {
		java.sql.Connection connection = null;
		java.sql.PreparedStatement pStatement = null;
		ResultSet rSet = null;
		List<Artical> articals = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnect();
			String sql = "select * from artical where  text like ? or author like ?";
			pStatement = connection.prepareStatement(sql);
			System.out.println(word);
			pStatement.setString(1, "%"+word+"%");
			pStatement.setString(2, "%"+word+"%");
			rSet = pStatement.executeQuery();
			while(rSet.next()) {
				Artical artical = new Artical();
				artical.setAuthor(rSet.getString(1));
				artical.setLordId(rSet.getInt(2));
				artical.setText(rSet.getString(3));
				artical.setTime(rSet.getString(4));
				artical.setFloor(Integer.toString(rSet.getInt(5)));
				artical.setId(rSet.getInt(6));
				articals.add(artical);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(connection, pStatement, rSet);
		}
		return articals;
	}

	@Override
	public void deleteArtical(String lordId) {
		java.sql.Connection connection = null;
		java.sql.PreparedStatement pStatement = null;
		ResultSet rSet = null;
		try {
			connection = JDBCUtil.getConnect();
			String sql = "delete from artical where lordId = ?";
			pStatement = connection.prepareStatement(sql);
			pStatement.setInt(1, Integer.parseInt(lordId));
			pStatement.executeUpdate();
			LikeDao likeDao = new LikeDaoImlo();
			likeDao.delLike(lordId);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(connection, pStatement, rSet);
		}
	}

	@Override
	public void deleArtByFloor(String lordId, String floor) {
		java.sql.Connection connection = null;
		java.sql.PreparedStatement pStatement = null;
		ResultSet rSet = null;
		try {
			connection = JDBCUtil.getConnect();
		
			String sql = "delete from artical where lordId = ? and floor = ?";
			pStatement = connection.prepareStatement(sql);
			pStatement.setInt(1, Integer.parseInt(lordId));
			pStatement.setInt(2, Integer.parseInt(floor));
			pStatement.executeUpdate();
			
			LikeDao likeDao = new LikeDaoImlo();
			likeDao.delLike(lordId);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.closeAll(connection, pStatement, rSet);
		}
	}

	

}
